<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
	<link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
	<link rel="stylesheet" href="/css/treetable/jquery.treetable.css" />
	<link rel="stylesheet" href="/css/treetable/jquery.treetable.theme.default.css" />
	<script src="/js/libs/jquery-2.1.1.min.js"></script>
	<link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
	<script src="/js/bootstrap/bootstrap.min.js"></script>
	<script src="/js/bootstrap/bootstrap-select.min.js"></script>
	<script src="/js/jq.js"></script>
	<script src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
	<script src="/js/common.js"></script>
	<script src="/layui/layui.js"></script>
	<script src="/js/dict.js"></script>
	<script src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
	<script src="/js/my/ztree-menu.js"></script>
	<script src="/js/my/permission.js"></script>
	<script src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
	<script src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
	<script src="/js/libs/jquery.treetable.js"></script>
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-md-offset-2 col-lg-offset-2">
	<form class="form-horizontal" onsubmit="return false" id="form">
		<fieldset>
			<div class='form-group'>
				<label class='col-md-4 control-label'>上级部门</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='parentId' id='parentId'>
					<input class='form-control' type='hidden' name='parentIds' id='parentIds'>
					<input style="width: 90%; float: left" class='form-control' type='text'
						   name='parentName' id='parentName' placeholder='上级部门'
						   data-bv-notempty='true' data-bv-notempty-message='上级部门 不能为空' readonly >
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
							onclick="showSpecialTree('parentId', 'parentName', 'dept', '部门', '总公司', 'select','fasDeptTree')">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
				<label class='col-md-4 control-label'>所属公司</label>
			</div>
			<div class='form-group'>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="company" id="company"
							title="所属公司" data-live-search="false" data-bv-notempty='true'
							data-bv-notempty-message='所属公司 不能为空'></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>机构名称</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='机构名称' type='text' name='name' id='name'
						   data-bv-notempty='true' data-bv-notempty-message='机构名称 不能为空'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>机构类型</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="type" id="type"
							title="机构类型" data-live-search="true"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>机构等级</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="level" id="level"
							title="机构等级" data-live-search="true"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>是否公共模块使用</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="isPublic" id="isPublic"
							title="是否公共模块使用" data-live-search="true" disabled="disabled"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>是否资产模块使用</label>
				<div class='col-md-8'>
					<select class="form-control input-sm selectpicker show-tick" name="isFas" id="isFas"
							title="是否资产模块使用" data-live-search="true" disabled="disabled"></select>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>主负责人</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='primaryUserId' id='primaryUserId'>
					<input style="width: 90%;float: left;" class='form-control' placeholder='主负责人' type='text'
						   name='primaryUserName' id='primaryUserName' readonly>
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
							onclick="showTableAndFlushFields('user', 'user', '用户', true, false, 'primaryUserId:id', 'primaryUserName:nickname')">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>副负责人ID</label>
				<div class='col-md-8'>
					<input class='form-control' type='hidden' name='deputyUserId' id='deputyUserId' >
					<input style="width: 90%;float: left;" class='form-control' placeholder='副负责人' type='text'
						   name='deputyUserName' id='deputyUserName' readonly>
					<button style="width: 10%;float: right; height: 32px; line-height: 32px;" class="layui-btn layui-btn-sm"
							onclick="showTableAndFlushFields('user', 'user', '用户', true, false, 'deputyUserId:id', 'deputyUserName:nickname')">
						<i class="layui-icon">&#xe615;</i>
					</button>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>排序</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='排序' type='text' name='sort' id='sort' value="100"
						   data-bv-notempty='true' data-bv-notempty-message='排序 不能为空'>
				</div>
			</div>
			<div class='form-group'>
				<label class='col-md-4 control-label'>备注</label>
				<div class='col-md-8'>
					<input class='form-control' placeholder='备注' type='text' name='remark' id='remark'>
				</div>
			</div>


			<div class="form-actions">
				<div class="row" align="center">
					<div class="col-md-12">
						<button class="btn btn-primary" type="submit" onclick="save()">
							<i class="fa fa-save"></i> 保存
						</button>
					</div>
				</div>
			</div>

		</fieldset>
	</form>
</div>

<script type="text/javascript">
    $(function () {
        $('#form').bootstrapValidator();
    });
    layui.use(['layer','laydate'], function(){
        var layer = layui.layer;
    });

    showDictSelect("type", "dept_type");
    showDictSelect("level", "dept_level");
	showDictSelect("isPublic", "boolean",'','0');
	showDictSelect("isFas", "boolean",'','1');
	showDictSelect('company', 'company', '', 'beep');

    var parentId = getUrlParam('parentId') ? getUrlParam('parentId') : 0;
    var parentName = getUrlParam('parentName') ? getUrlParam('parentName') : 'root';
    var isSelected = false; // 树结构是否选择

    var primaryUserId = '';
    var primaryUserName = '';

    $('#parentId').val(parentId);
    $('#parentName').val(parentName);

    // 保存
    function save() {

		$("select[disabled]").each(function() {
			if (parseInt($(this).val()) != -1) {
				$(this).attr("disabled", false);
			}
		});

        $('#form').bootstrapValidator();
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if(!bootstrapValidator.isValid()){
            return;
        }

        var formdata = $("#form").serializeObject();

        $.ajax({
            type : 'post',
            url : '/dept',
            contentType: "application/json; charset=utf-8",
            data : JSON.stringify(formdata),
            success : function(json) {
                //console.log(json);
                if (json.code === 0) {
                    layer.msg("添加成功", {shift: -1, time: 1000}, function () {
                        // location.href = "deptList.html";
                        parent.location.reload();//关闭iFrame
                    });
                } else {
                    layer.msg("添加失败：" + json.message, {shift: -1, time: 1000});
                }

            }
        });
    }

</script>
</body>
</html>
